#!/usr/bin/env python
# -*- coding: utf-8 -*-

from OsmData import *

def generate_scene(filename):
  data = OsmData()
  f = open(filename, "r")
  data.read(f)
  f.close()
  buildings = []
  merc = math.cos(math.radians(data.bbox[1]))
  mbboxsw = c4326t3857(data.bbox[0],data.bbox[1])
  mbboxne = c4326t3857(data.bbox[2],data.bbox[3])
  mbbox = [mbboxsw[0], mbboxsw[1], mbboxne[0], mbboxne[1]]
  dx = (mbboxne[0]-mbboxsw[0]) / 2.0
  dy = (mbboxne[1]-mbboxsw[1]) / 2.0
  midlon = mbboxsw[0] + dx
  midlat = mbboxsw[1] + dy
  for way in data.ways.values():
    if (way[TAG].get("building") != None or way[TAG].get("building:part") != None):
      nodelist = []
      minh = 0
      maxh = 3.0
      color = [0.73, 0.66, 0.66, 1.0]
      if (way[TAG].get("building:part:height") != None):
        maxh = float(way[TAG].get("building:part:height"))
      elif (way[TAG].get("height") != None):
        maxh = float(way[TAG].get("height"))
      elif (way[TAG].get("building:levels") != None):
        maxh = float(way[TAG].get("building:levels")) * 3.0 + 4.0
      if (way[TAG].get("min_height") != None):
        minh = float(way[TAG].get("min_height"))
      elif (way[TAG].get("building:min_level") != None):
        minh = float(way[TAG].get("building:min_level")) * 3.0 + 2.0
      nodelist.append((minh / merc, maxh / merc, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
    elif (way[TAG].get("landuse") == "residential"):
      nodelist = []
      color = [0.87, 0.87, 0.87, 1.0]
      nodelist.append((-0.2 / merc, 0.0, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
    elif (way[TAG].get("landuse") == "industrial"):
      nodelist = []
      color = [0.87, 0.82, 0.84, 1.0]
      nodelist.append((-0.2 / merc, 0.0, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
    elif (way[TAG].get("natural") == "wood"):
      nodelist = []
      color = [0.68, 0.82, 0.63, 1.0]
      nodelist.append((0.0, 15.0 / merc, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
    elif (way[TAG].get("natural") == "water") or (way[TAG].get("waterway") == "riverbank"):
      nodelist = []
      color = [0.352, 0.63, 0.71, 0.38]
      nodelist.append((-15.0 / merc, 0.0, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
    elif (way[TAG].get("landuse") == "forest"):
      nodelist = []
      color = [0.55, 0.77, 0.42, 1.0]
      nodelist.append((0.0, 15.0 / merc, color[0], color[1], color[2], color[3]))
      for nodeid in way[REF]:
        mnode = c4326t3857(data.nodes[nodeid][LON], data.nodes[nodeid][LAT])
        nodelist.append((mnode[0] - midlon, mnode[1] - midlat))
      buildings.append(nodelist)
  return buildings

  # wgs84 -> utm
def c4326t3857(lon, lat):
  lat_rad = math.radians(lat)
  xtile = lon * 20037508.342789244 / 180
  ytile = math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad)))/math.pi * 20037508.342789244
  return(xtile, ytile)
